package com.andaily.domain.dto.account;

import com.andaily.domain.account.AccountType;
import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.MobileStyle;
import com.andaily.domain.user.User;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class AccountTypeDTO extends AbstractDTO {

    private String name;
    private String description;
    private int usedTimes;

    private boolean defaultType;
    private boolean daily;
    private boolean publicity;
    private MobileStyle style = MobileStyle.defaultStyle();

    public AccountTypeDTO() {
    }

    public AccountTypeDTO(AccountType accountType) {
        this.guid = accountType.guid();
        this.name = accountType.name();
        this.description = accountType.description();

        this.usedTimes = accountType.usedTimes();
        this.defaultType = accountType.defaultType();
        this.daily = accountType.daily();

        this.publicity = accountType.publicity();
        this.style = accountType.style();
    }

    public AccountTypeDTO(boolean daily, boolean publicity) {
        this.daily = daily;
        this.publicity = publicity;
    }

    public MobileStyle[] getAvailableStyles() {
        return MobileStyle.values();
    }

    public boolean isEditable() {
        return StringUtils.isNotEmpty(guid);
    }

    public String getName() {
        return name;
    }

    public MobileStyle getStyle() {
        return style;
    }

    public void setStyle(MobileStyle style) {
        this.style = style;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getUsedTimes() {
        return usedTimes;
    }

    public void setUsedTimes(int usedTimes) {
        this.usedTimes = usedTimes;
    }

    public boolean isDefaultType() {
        return defaultType;
    }

    public void setDefaultType(boolean defaultType) {
        this.defaultType = defaultType;
    }

    public boolean isDaily() {
        return daily;
    }

    public void setDaily(boolean daily) {
        this.daily = daily;
    }

    public boolean isPublicity() {
        return publicity;
    }

    public void setPublicity(boolean publicity) {
        this.publicity = publicity;
    }

    public static List<AccountTypeDTO> toDTOList(List<AccountType> accountTypes) {
        List<AccountTypeDTO> list = new ArrayList<AccountTypeDTO>(accountTypes.size());
        for (AccountType accountType : accountTypes) {
            list.add(new AccountTypeDTO(accountType));
        }
        return list;
    }

    public AccountType toType(User user) {
        AccountType accountType = new AccountType(name, description, false);
        accountType.update(user, daily, publicity);
        accountType.updateStyle(this.style);
        return accountType;
    }
}
